package com.luckku.boot.controller;


import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.Exam;
import com.luckku.boot.entity.User;
import com.luckku.boot.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    ExamService examService;

    /**
     * /exam/addExam
     * 新建考试
     *
     * @param exam Exam
     * @return ApiResult<Map < String, Object>>
     */
    @PostMapping(value = "/addExam")
    public ApiResult<Map<String, Object>> addExam(@Valid @RequestBody Exam exam) {
        boolean save = examService.save(exam);
        if (save) {
            Map<String, Object> map = new HashMap<>(16);
            map.put("map", exam);
            return ApiResult.success(map, "添加成功");
        }
        return ApiResult.failed("添加失败，请检查考试信息");
    }

    /**
     * /exam/getAllExams
     * 获取所有考试信息
     *
     * @return ApiResult<List < Exam>>
     */
    @GetMapping(value = "/getAllExams")
    public ApiResult<List<Exam>> getAllExams() {
        List<Exam> examList = examService.list();
        return ApiResult.success(examList);
    }

    /**
     * /exam/getExamById
     * 获取考试（根据id）
     *
     * @param examId String
     * @return ApiResult<Exam>
     */
    @GetMapping(value = "/getExamById")
    public ApiResult<Exam> getExamById(@RequestParam(value = "examId") String examId) {
        Exam exam = examService.getById(examId);
        return ApiResult.success(exam);
    }

    /**
     * /exam/updateExam
     * 考试信息
     *
     * @return ApiResult<Exam>
     */
    @PutMapping(value = "/updateExam")
    public ApiResult<Exam> updateUserInfo(@RequestBody Exam exam) {
        boolean b = examService.updateById(exam);
        if (b) {
            return ApiResult.success(examService.getById(exam.getExamId()));
        }
        return ApiResult.failed("更新失败");
    }
}

